package com.hevs.samplewebapp.client.dialogbox;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Dialog box for confirmation
 * <p>It provides a dialog box to confirm or cancel a update</p>
 * @author Lionel Rey
 * @date 26.03.2012
 *
 */
public class ConfirmDialogBox extends DialogBox{
	
	// ------------------------------------------------------------
	// VARIABLES
	// ------------------------------------------------------------
	
	/**
	 * Boolean for the YES
	 */
	public static final boolean YES = true;
	
	/**
	 * Boolean for the NO
	 */
	public static final boolean NO = false;
	
	/**
	 * Vertical panel is the main panel of the dialog box
	 */
	private VerticalPanel main = new VerticalPanel();
	
	/**
	 * Horizontal panel for the buttons
	 */
	private HorizontalPanel buttonsPanel = new HorizontalPanel();
	
	/**
	 * Array of Strings of messages to display
	 */
	private String[] messages;
	
	/**
	 * Title of the dialog box
	 */
	private String title;
	
	/**
	 * Default boolean choice
	 */
	private boolean choice = YES;
	
	// ------------------------------------------------------------
	// CONSTRUCTORS
	// ------------------------------------------------------------
	
	/**
	 * Constructor for the dialog box
	 * @param title
	 * 	titel of the box
	 * @param message
	 * 	messages to display in the dialog box
	 */
	public ConfirmDialogBox(String title, String[] message){
		super(false,false);	// modale and not auto-hide
		this.messages = message;
		this.title = title;
		initDialogBox();
	}
	
	// ------------------------------------------------------------
	// METHODS
	// ------------------------------------------------------------

	/**
	 * Getter for the choice
	 * @return
	 * the choice in boolean format
	 */
	public boolean getChoice(){
		return choice;
	}
	
	/**
	 * Method to initialize the widget
	 */
	private void initDialogBox() {
		this.setText(title);
		for(String m : messages)
			main.add(new Label(m));
		
		Button btnYes = new Button("Yes");
		Button btnNo = new Button("No");
		buttonsPanel.add(btnYes);
		buttonsPanel.add(btnNo);
		main.add(buttonsPanel);
		
		buttonsPanel.setSize("100%", "100%");
		
		this.setWidget(main);

		btnYes.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				choice  = ConfirmDialogBox.YES;
				ConfirmDialogBox.this.hide();
			}
		});
		
		btnNo.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				choice  = ConfirmDialogBox.NO;
				ConfirmDialogBox.this.hide();
			}
		});
	}
}
